﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Web.UI.Design;
using System.Web.UI.Design.WebControls;
using System.Web.UI.WebControls;

namespace MyLib.Web.UI {
	/// <summary>
	/// 
	/// </summary>
	public class MyFormViewDesigner : DataBoundControlDesigner {
		private MyFormViewDesignerActionList m_actionLists;

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		private IDataSourceViewSchema GetDataSourceSchema() {
			DesignerDataSourceView designerView = base.DesignerView;
			if (designerView != null) {
				try {
					return designerView.Schema;
				} catch (Exception exception) {
					IComponentDesignerDebugService service = (IComponentDesignerDebugService)base.Component.Site.GetService(typeof(IComponentDesignerDebugService));
					if (service != null) {
						service.Fail(string.Format("Cannot get schema of data source. {0}", exception.Message));
					}
				}
			}
			return null;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public override string GetDesignTimeHtml() {
			var viewControl = (MyFormView)base.ViewControl;
			bool flag = false;
			string[] dataKeyNames = null;
			if (this.CurrentModeTemplateExists) {
				bool flag2 = false;
				IDataSourceViewSchema dataSourceSchema = this.GetDataSourceSchema();
				if (dataSourceSchema != null) {
					IDataSourceFieldSchema[] fields = dataSourceSchema.GetFields();
					if ((fields != null) && (fields.Length > 0)) {
						flag2 = true;
					}
				}
				try {
					if (!flag2) {
						dataKeyNames = viewControl.DataKeyNames;
						viewControl.DataKeyNames = new string[0];
						flag = true;
					}
					TypeDescriptor.Refresh(base.Component);
					return base.GetDesignTimeHtml();
				} finally {
					if (flag) {
						viewControl.DataKeyNames = dataKeyNames;
					}
				}
			}
			return this.GetEmptyDesignTimeHtml();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="component"></param>
		public override void Initialize(IComponent component) {
			base.Initialize(component);
			SetViewFlags(ViewFlags.TemplateEditing, true);
		}

		/// <summary>
		/// 
		/// </summary>
		protected override void OnSchemaRefreshed() {
			if (!base.InTemplateMode) {
				ControlDesigner.InvokeTransactedChange(base.Component, new TransactedChangeCallback(this.SchemaRefreshedCallback), null, "Schema refresh.");
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="context"></param>
		/// <returns></returns>
		private bool SchemaRefreshedCallback(object context) {
			this.UpdateDesignTimeHtml();
			return true;
		}

		/// <summary>
		/// 
		/// </summary>
		public override DesignerActionListCollection ActionLists {
			get {
				DesignerActionListCollection lists = new DesignerActionListCollection();
				lists.AddRange(base.ActionLists);
				if (this.m_actionLists == null) {
					this.m_actionLists = new MyFormViewDesignerActionList(this);
				}
				bool inTemplateMode = base.InTemplateMode;
				DesignerDataSourceView designerView = base.DesignerView;
				lists.Add(this.m_actionLists);
				return lists;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		private bool CurrentModeTemplateExists {
			get {
				var itemTemplate = ((MyFormView)ViewControl).ContentTemplate;

				if (itemTemplate == null) {
					return false;
				}

				IDesignerHost service = (IDesignerHost)base.ViewControl.Site.GetService(typeof(IDesignerHost));
				string str = ControlPersister.PersistTemplate(itemTemplate, service);
				return ((str != null) && (str.Length > 0));
			}
		}

		/// <summary>
		/// 
		/// </summary>
		protected override int SampleRowCount {
			get {
				return 1;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public override TemplateGroupCollection TemplateGroups {
			get {
				TemplateGroupCollection templateGroups = base.TemplateGroups;

				var style = new Style();
				var cstyle = ((MyFormView)this.Component).ContentStyle;
				if (cstyle != null) {
					style.CopyFrom(cstyle);
				} else {
					var astyle = ((MyFormView)this.Component).ControlStyle;
					if (astyle != null) {
						style.CopyFrom(astyle);
					}
				}

				TemplateGroup group = new TemplateGroup("ContentTemplate", style);
				TemplateDefinition templateDefinition = new TemplateDefinition(this, "ContentTemplate", base.Component, "ContentTemplate");
				templateDefinition.SupportsDataBinding = true;
				group.AddTemplateDefinition(templateDefinition);
				templateGroups.Add(group);

				return templateGroups;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		protected override bool UsePreviewControl {
			get {
				return true;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		protected internal string Title {
			get {
				var panel = Component as MyFormView;
				if (panel == null) {
					return string.Empty;
				} else {
					return panel.Title;
				}
			}
			set {
				var panel = Component as MyFormView;
				if (panel == null) {
					return;
				} else if(value != this.Title) {
					TypeDescriptor.Refresh(panel);

					TypeDescriptor.GetProperties(typeof(MyFormView))["Title"].SetValue(Component, value);
				}
			}
		}

		/// <summary>
		/// 
		/// </summary>
		protected internal string SkinId {
			get {
				var panel = Component as MyFormView;
				if (panel == null) {
					return string.Empty;
				} else {
					return panel.SkinID;
				}
			}
			set {
				var panel = Component as MyFormView;
				if (panel == null) {
					return;
				} else if (value != this.SkinId) {
					TypeDescriptor.Refresh(panel);

					TypeDescriptor.GetProperties(typeof(MyFormView))["SkinID"].SetValue(Component, value);
				}
			}
		}
	} // end of MyFormViewDesigner.

	/// <summary>
	/// 
	/// </summary>
	internal class MyFormViewDesignerActionList : DesignerActionList {
		private MyFormViewDesigner m_designer;

		/// <summary>
		/// 
		/// </summary>
		/// <param name="designer"></param>
		public MyFormViewDesignerActionList(MyFormViewDesigner designer)
			: base(designer.Component) {
			m_designer = designer;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public override DesignerActionItemCollection GetSortedActionItems() {
			var items = new DesignerActionItemCollection();

			var itemTitle = new DesignerActionPropertyItem("Title", Properties.Main.MyPanel_Title, "Appearance", Properties.Main.MyPanel_Title_Description);
			itemTitle.RelatedComponent = m_designer.Component;

			var itemSkinId = new DesignerActionPropertyItem("SkinId", Properties.Main.MyPanel_SkinId, "Appearance", Properties.Main.MyPanel_SkinId_Description);
			itemSkinId.RelatedComponent = m_designer.Component;

			items.Add(itemTitle);
			items.Add(itemSkinId);

			return items;
		}

		/// <summary>
		/// 
		/// </summary>
		public string Title {
			get {
				if (m_designer == null) {
					return string.Empty;
				} else {
					return m_designer.Title;
				}
			}
			set {
				if (m_designer == null) {
					return;
				} else {
					m_designer.Title = value;
				}
			}
		}

		/// <summary>
		/// 
		/// </summary>
		[TypeConverter(typeof(SkinIDTypeConverter))]
		public string SkinId {
			get {
				if (m_designer == null) {
					return string.Empty;
				} else {
					return m_designer.SkinId;
				}
			}
			set {
				if (m_designer == null) {
					return;
				} else {
					m_designer.SkinId = value;
				}
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public override bool AutoShow {
			get {
				return true;
			}
			set {
			}
		}
	} // end of MyFormViewDesignerActionList.
}
